package com.tiantianyuegou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.tiantianyuegou.activity.BaseActivity;
import com.tiantianyuegou.R;
import com.tiantianyuegou.app.SysApplication;
import com.tiantianyuegou.enums.PayType;
import com.tiantianyuegou.fragment.OrderListFragment;
import com.tiantianyuegou.http.SPHelper;
import com.tiantianyuegou.pay.OnMyPayListner;
import com.tiantianyuegou.util.Tools;
import com.tiantianyuegou.view.PagerSlidingTabStrip;

import org.json.JSONArray;

/**
 * 我的订单
 */
public class MyOrderActivity extends BaseActivity implements View.OnClickListener,OnMyPayListner{

    private ViewPager viewPager;
    private PagerSlidingTabStrip tabStrip;
    private ImageView mIMg_back;
    private TextView mTxt_title;
    private String TAG = "MyOrderActivity";
    private JSONArray shopList;
    private boolean[] fragmentsUpdateFlag = {false, false, false, false};

    private SysApplication application;

    private String mType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order);
      Tools.changeSystemBar(this);
        initViews();

    }

    @Override
    protected void onResume() {
        super.onResume();
        //清空之情的activity栈
        application = SysApplication.getInstance();
        if ("share".equals(mType)) {
            if (application.mlist.size() > 0) {
                application.exit();
            }
        }
    }

    @Override
    public void findViewByIds() {
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        tabStrip = (PagerSlidingTabStrip) findViewById(R.id.tabstrip);
        mIMg_back = (ImageView) findViewById(R.id.img_title);
        mTxt_title = (TextView) findViewById(R.id.text_title);
    }

    @Override
    public void initListener() {
        viewPager.setAdapter(new Adapter(getSupportFragmentManager()));
        tabStrip.setViewPager(viewPager);
        mIMg_back.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mType = getIntent().getStringExtra("type");
        if ("member".equals(mType) || "share".equals(mType)) {
            mTxt_title.setText("我的订单");
            SPHelper.setTYPE("member");
        } else if ("seller".equals(mType)) {
            mTxt_title.setText("订单管理");
            SPHelper.setTYPE("seller");
        }

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_title:
                if ("share".equals(mType)) {
                    Intent intent = new Intent(this, MainActivity.class);
                    intent.putExtra("type", "order");
                    startActivity(intent);
                    if(Integer.valueOf(android.os.Build.VERSION.SDK) > 4 ){
                        overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
                    }
                }
                finish();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if ("share".equals(mType)) {
                Intent intent = new Intent(this, MainActivity.class);
                intent.putExtra("type", "order");
                startActivity(intent);
                if(Integer.valueOf(android.os.Build.VERSION.SDK) > 4 ){
                    overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
                }
            }
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onPayStart(PayType var1) {

    }

    @Override
    public void onPaySuccessed(PayType var1, String var2) {

    }

    @Override
    public void onPayFailed(PayType var1, String var2) {

    }

    @Override
    public void onPayCanceled(PayType var1) {

    }

    @Override
    public void onPayConfirming(PayType var1) {

    }

    class Adapter extends FragmentPagerAdapter {

        private FragmentManager fm;
        private final String[] TITLES = {"全部", "待付款", "待发货", "待收货", "待评价",
//                "退货退款"
        };


        public Adapter(FragmentManager fm) {
            super(fm);
            this.fm = fm;
            //Log.e(TAG, "FragmentPagerAdapter     Adapter");
        }

        @Override
        public Fragment getItem(int position) {
            (new OrderListFragment()).shopArray = shopList;
            //Log.e(TAG, "FragmentPagerAdapter     getItem      "+position);
            // (new OrderListFragment()).load(position);
            return OrderListFragment.newInstance(position, shopList);
        }

        @Override
        public int getCount() {
            //Log.e(TAG, "FragmentPagerAdapter     getCount"+"    "+TITLES.length);
            return TITLES.length;
        }


        @Override
        public CharSequence getPageTitle(int position) {
            // Log.e(TAG, "FragmentPagerAdapter     getPageTitle    "+position);
            (new OrderListFragment()).load(position);
            return TITLES[position];
        }

//        @Override
//        public Object instantiateItem(ViewGroup container, int position) {
//            //得到缓存的fragment
//            Fragment fragment = (Fragment) super.instantiateItem(container,
//                    position);
////得到tag，这点很重要
//            String fragmentTag = fragment.getTag();
//
//
//            if (fragmentsUpdateFlag[position % fragmentsUpdateFlag.length]) {
////如果这个fragment需要更新
//
//                FragmentTransaction ft = fm.beginTransaction();
////移除旧的fragment
//                ft.remove(fragment);
////换成新的fragment
//                fragment = OrderListFragment.newInstance(position, shopList);
////添加新fragment时必须用前面获得的tag，这点很重要
//                ft.add(container.getId(), fragment, fragmentTag);
//                ft.attach(fragment);
//                ft.commit();
//
////复位更新标志
//                fragmentsUpdateFlag[position % fragmentsUpdateFlag.length] = false;
//            }
//
//
//            return fragment;
//
//        }
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
    }
}

